package com.zhiche.lisa.bms.service.invoice.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.zhiche.lisa.bms.dao.mapper.invoice.InvoiceApplyBillMapper;
import com.zhiche.lisa.bms.dao.model.bill.BillIo;
import com.zhiche.lisa.bms.dao.model.invoice.InvoiceApplyBill;
import com.zhiche.lisa.bms.service.bill.BillIoService;
import com.zhiche.lisa.bms.service.bill.BillService;
import com.zhiche.lisa.bms.service.invoice.InvoiceApplyBillService;
import com.zhiche.lisa.core.supports.BaseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 申请开票明细-账单 服务实现类
 * </p>
 *
 * @author liuanshun
 * @since 2018-09-27
 */
@Service
public class InvoiceApplyBillServiceImpl extends ServiceImpl<InvoiceApplyBillMapper, InvoiceApplyBill> implements InvoiceApplyBillService {
    @Autowired
    private BillService billService;

    @Autowired
    private BillIoService billIoService;

    /**
     * 根据头单id获取申请账单开票明细
     *
     * @param headerId
     * @return
     */
    @Override
    public List<BillIo> selectInvoiceApplyBillListByHeaderId(Long headerId) {
        try {
            //验证传入参数
            if (Objects.isNull(headerId)) {
                throw new BaseException("请传入需要匹配的id");
            }
            EntityWrapper<InvoiceApplyBill> invoiceApplyBillEw = new EntityWrapper<>();
            invoiceApplyBillEw.eq("head_id", headerId);
            List<InvoiceApplyBill> invoiceApplyBills = this.selectList(invoiceApplyBillEw);
            List<String> codes = new ArrayList<>();
            if (!CollectionUtils.isEmpty(invoiceApplyBills)) {
                invoiceApplyBills.forEach(invoiceApplyBill -> codes.add(invoiceApplyBill.getBillCode()));
            }
            //return billService.selectBillListByCustomerBillCode(codes);
            if (!CollectionUtils.isEmpty(codes)) {
                return billIoService.selectBillListByBillCode(codes);
            } else {
                return null;
            }
        } catch (BaseException e) {
            throw e;
        } catch (Exception e) {
            throw new BaseException("获取申请账单开票明细异常");
        }
    }

    /**
     * 批量新增申请账单开票详明
     *
     * @param invoiceApplyBills 账单批量信息
     * @param headerId          对应申请帐单id
     * @return
     */
    @Override
    public void insertInvoiceApplyBill(List<InvoiceApplyBill> invoiceApplyBills, Long headerId) {
        try {
            if (Objects.isNull(headerId)) {
                throw new BaseException("请传入需要匹配的id");
            }
            if (!CollectionUtils.isEmpty(invoiceApplyBills)) {
                for (InvoiceApplyBill invoiceApplyAsse :
                        invoiceApplyBills) {
                    invoiceApplyAsse.setHeadId(headerId);
                }
                this.insertBatch(invoiceApplyBills);
            }
        } catch (BaseException e) {
            throw e;
        } catch (Exception e) {
            throw new BaseException("申请账单开票详明新增异常");
        }
    }

    /**
     * 删除
     *
     * @param headId
     */
    @Override
    public void deleteInvoiceApplyBillByHeadId(Long headId) {
        if (Objects.nonNull(headId)) {
            EntityWrapper<InvoiceApplyBill> ew = new EntityWrapper<>();
            ew.eq("head_id", headId);
            this.delete(ew);
        }
    }
}
